package com.self.smallma.myblogweb.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.self.smallma.myblogweb.common.utils.PageUtils;
import com.self.smallma.myblogweb.common.utils.Query;
import com.self.smallma.myblogweb.dao.BlogDao;
import com.self.smallma.myblogweb.entity.BlogEntity;
import com.self.smallma.myblogweb.service.BlogService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.self.smallma.myblogweb.dao.TypeDao;
import com.self.smallma.myblogweb.entity.TypeEntity;
import com.self.smallma.myblogweb.service.TypeService;
import org.springframework.util.StringUtils;


@Service("typeService")
public class TypeServiceImpl extends ServiceImpl<TypeDao, TypeEntity> implements TypeService {

    @Autowired
    BlogService blogService;
    @Autowired
    TypeDao typeDao;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TypeEntity> page = this.page(
                new Query<TypeEntity>().getPage(params),
                new QueryWrapper<TypeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<TypeEntity> getAllTypeAndBlog() {


//        String typeJson = redisTemplate.opsForValue().get("typeJson");
//        if (StringUtils.isEmpty(typeJson)){
            //查找数据库
            List<TypeEntity> typeEntity = typeDao.getAllTypeAndBlog();
//            添加到缓存
//            String s = JSON.toJSONString(typeEntity);
//            redisTemplate.opsForValue().set("typeJson",s,3, TimeUnit.HOURS);
            return typeEntity;
//        }
//        List<TypeEntity> result = JSON.parseObject(typeJson,new TypeReference<List<TypeEntity>>(){});


//        return result;

    }

}